package com.crack.nga2.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.crack.nga2.R;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.WorkHandler;

import java.util.Map;

public class SettingActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, DialogInterface.OnClickListener {
    private ListView listView;
    private String[] items;
    private String cacheSize = "0KB";
    private SharedPreferences sp;
    private Handler workHandler = new WorkHandler();
    private Map<String, Boolean> map = GlobalConst.settings;
    private Handler mainHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        items = getResources().getStringArray(R.array.settings);
        listView = (ListView) findViewById(R.id.listView);
        long size = sp.getLong(items[8], 0);
        cacheSize = size + "KB";
        listView.setAdapter(new SettingAdapter());
        listView.setOnItemClickListener(SettingActivity.this);

    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finish();
                break;
            case R.id.iv_switch:
                ImageView iv = (ImageView) view;
                int position = (int) view.getTag();
                final String item = items[position];
                final boolean b = !map.get(item);
                iv.setImageResource(b ? R.drawable.switch_bt_open : R.drawable.switch_bt_close);
                map.put(item, b);
                sp.edit().putBoolean(item, b).apply();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                //账号管理
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                break;
            case 8:
                new AlertDialog.Builder(this).setTitle("清除缓存").setMessage("是否清除缓存，清除后无法恢复!")
                        .setPositiveButton("确定", this).setNegativeButton("取消", null).show();
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                workHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //清除缓存
                        //TODO
                        View view = listView.getChildAt(8);
                        final TextView cacheView = (TextView) view.findViewById(R.id.cache_size);
                        //计算混村
                        final long size = 0;
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                cacheView.setText(size + "KB");
                                Toast.makeText(SettingActivity.this, "清除缓存成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                break;
        }
    }

    private class SettingAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            @SuppressLint("ViewHolder")
            View view = View.inflate(SettingActivity.this, R.layout.activity_setting_item, null);
            String item = items[position];
            TextView text = (TextView) view.findViewById(R.id.text);
            TextView cache = (TextView) view.findViewById(R.id.cache_size);
            ImageView switcher = (ImageView) view.findViewById(R.id.iv_switch);
            text.setText(item);
            switch (position) {
                case 0:
                case 9:
                case 10:
                    switcher.setVisibility(View.GONE);
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    switcher.setVisibility(View.VISIBLE);
                    switcher.setTag(position);
                    boolean b = map.get(item);
                    switcher.setImageResource(b ? R.drawable.switch_bt_open : R.drawable.switch_bt_close);
                    break;
                case 8:
                    //缓存
                    switcher.setVisibility(View.GONE);
                    cache.setText(cacheSize);
                    break;

            }
            return view;
        }
    }
}
